package com.sm.honkon.ui.model

import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.viewModelScope
import com.sm.honkon.app.App
import com.sm.honkon.app.ext.getTimeFormat
import com.sm.honkon.data.bean.ControlLogCatEntity
import com.sm.honkon.data.bean.CustomerEntity
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
import me.hgj.jetpackmvvm.base.viewmodel.BaseViewModel
import me.hgj.jetpackmvvm.callback.databind.BooleanObservableField
import me.hgj.jetpackmvvm.callback.databind.IntObservableField
import me.hgj.jetpackmvvm.callback.databind.StringObservableField
import me.hgj.jetpackmvvm.callback.livedata.FloatLiveData
import me.hgj.jetpackmvvm.callback.livedata.IntLiveData
import me.hgj.jetpackmvvm.ext.request

class MainViewModel : BaseViewModel() {
    private val logcatDao = App.database.logcatDao()
    val customerDao = App.database.customerDao()
    val timeText = StringObservableField()
    val hint = StringObservableField("")
    val centerHint = StringObservableField("准备")
    val handlername = StringObservableField("admin")
    val errorHint = StringObservableField("")
    val guangbanmianji = IntLiveData(0)
    val curCustomerEntity = MutableLiveData<CustomerEntity>()
    val controlLogCat = MutableLiveData<ControlLogCatEntity>()

    var count = 20000
    val runCount = IntObservableField(20000)
    val isCountDown = BooleanObservableField(false)

    val light = IntLiveData(1)
    val isYuran = BooleanObservableField(false)
    val isReady = BooleanObservableField(false)
    val isRun = BooleanObservableField(false)
    val canSelectPS = BooleanObservableField(true)
    val canSelectNS = BooleanObservableField(true)
    val selectPS = BooleanObservableField(true)


    val mJ = IntLiveData(50)
    val nengliangmidu = FloatLiveData(0f)
    val select1064 = BooleanObservableField(true)


    fun insertLogcat() {
        controlLogCat.value ?: return
        viewModelScope.launch(Dispatchers.IO) {
            logcatDao.insert(controlLogCat.value!!)
            curCustomerEntity.value?.let {
                it.treatment_count = ++it.treatment_count
                it.lastTime = System.currentTimeMillis().getTimeFormat("yyyy-MM-dd\nhh:mm:ss")
                customerDao.update(it)
            }
        }
    }
}